
package com.jojoteamwork.photo.util;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class ZipDataReader {
    public static String getText(String zipFilePath, String textFileName) {
        StringBuilder textContent = new StringBuilder();
        BufferedReader reader = null;
        ZipInputStream zis = null;
        try {
            FileInputStream fis = new FileInputStream(new File(zipFilePath));
            zis = new ZipInputStream(fis);
            ZipEntry ze = null;
            while ((ze = zis.getNextEntry()) != null) {

                if (ze.getName().equals(textFileName)) {

                    reader = new BufferedReader(new InputStreamReader(zis));
                    String c = null;
                    while ((c = reader.readLine()) != null) {
                        textContent.append(c).append("\n");
                    }

                }
                zis.closeEntry();
            }
        } catch (Exception e) {
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (zis != null) {
                try {
                    zis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

        return textContent.toString();
    }

    public static Bitmap getBitmapFromZip(String zipFilePath, String fileName) {
        ZipInputStream zis = null;
        Bitmap bitmap = null;
        try {
            FileInputStream fis = new FileInputStream(new File(zipFilePath));
            // ZipDecryptInputStream zds = new ZipDecryptInputStream(fis,
            // "123456");
            zis = new ZipInputStream(fis);
            ZipEntry ze = null;
            while ((ze = zis.getNextEntry()) != null) {
                if (ze.getName().equals(fileName)) {
                    bitmap = BitmapFactory.decodeStream(zis);
                }
                zis.closeEntry();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (zis != null) {
                try {
                    zis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return bitmap;
    }

    public static void copyDircetoryFromZip(Context context, String zipFilePath, String srcDirPath,
            String destDirPath) {

        ZipInputStream zis = null;
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        try {
            zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFilePath)));
            ZipEntry ze = null;
            String fileName;
            String path;
            while ((ze = zis.getNextEntry()) != null) {
                fileName = ze.getName();
                if (fileName.startsWith(srcDirPath + "/") && !ze.isDirectory()) {
                    path = destDirPath + fileName.substring(fileName.indexOf("/") + 1);
                    Utils.copyFile(path, zis, false);
                    intent.setData(Uri.parse("file://" + path));
                    context.sendBroadcast(intent);
                }
                zis.closeEntry();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (zis != null) {
                try {
                    zis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static ArrayList<String> getFileNamesInDirectory(String zipFilePath, String directory) {
        ArrayList<String> fileNamesList = new ArrayList<String>();
        try {
            ZipFile zipFile = new ZipFile(new File(zipFilePath));
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            ZipEntry entry;
            String entryName;
            while (entries.hasMoreElements()) {

                entry = (ZipEntry) entries.nextElement();
                entryName = entry.getName();
                if (entryName.startsWith(directory + "/") && !entry.isDirectory()) {
                    fileNamesList.add(entryName);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return fileNamesList;
    }

}
